package com.example.shopping.Orderitems.controller;


import com.example.shopping.JsonResult;
import com.example.shopping.Orderitems.entity.Orderitems;
import com.example.shopping.Orderitems.entity.Query;
import com.example.shopping.Orderitems.mapper.Orderitem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class OrderItems {
    @Autowired
    Orderitem Orderitem;

    //查询所有订单
    @RequestMapping("/api/Orderitems/List")
    public JsonResult getOrderitemsList(Query query) {
        int count=Orderitem.getOrderCount();
        query.setQuery("%"+query.getQuery()+"%");
        query.setPagenum((query.getPagenum()-1)* query.getPagesize());
        List<Orderitems> orderitems = Orderitem.getOrderitemsList(query);
        Map<String,Object> map = new HashMap<>();
        map.put("obj", orderitems);
        map.put("count", count);
        return new JsonResult(true, "查询成功", map);
    }

    //根具userid查询订单
    @RequestMapping("/api/Orderitems/UserList")
    public JsonResult getOrderitemsList_userid(int userid){
        List<Orderitems> UserList = Orderitem.getOrderitemsList_userid(userid);
        if(UserList != null) {
            return new JsonResult(true, "查询成功", UserList);
        }else{
            return new JsonResult(false ,"查询失败",null);
        }
    }
    //修改订单状态
    @RequestMapping("/api/Orderitems/UpdateStatus")
    public JsonResult UpdateOrderitemsStatus(int status,int id){
        int count = Orderitem.UpdateOrderitemsStatus(status,id);
        if (count==1){
            return new JsonResult(true, "修改成功", count);
        }else{
            return new JsonResult(false, "修改失败", count);
        }
    }



    //修改订单
    @RequestMapping("/api/Orderitems/Upd")
    public JsonResult Update(Double price,int quantity,int id){
        int msg = Orderitem.UpdateOrderitems(price, quantity, id);
        return new JsonResult(true,"修改成功",msg);
    }

    //添加订单
    @RequestMapping("/api/Orderitems/add")
    public JsonResult Add(Orderitems orderitems){
        SimpleDateFormat ft=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String time=ft.format(date);
        orderitems.setUpdated(time);
        int i = Orderitem.addOrderitems(orderitems);
        if( i == 1){
            return new JsonResult(true, "添加成功", i);
        }else {
            return new JsonResult(true, "添加失败", null);
        }
    }

    //模糊查询
    @RequestMapping("/api/Orderitems/likeList")
    public JsonResult getOrder_1(String goodsname){
        List<Orderitems> order = Orderitem.getOrder_1("%"+goodsname+"%");
//        System.out.println(order);
        return new JsonResult(true, "查询成功", order);
    }

    //删除订单
    @RequestMapping("/api/Orderitems/del_id")
    public JsonResult delOrderitems(int id){
        int del = Orderitem.delOrderitems(id);
        return new JsonResult(true,"删除成功",del);
    }
}
